﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Gx.Bll.Shared;
using Gx.Common;
using Gx.Dal.Employee;
using Gx.Model;
using Gx.Model.Entity;
using Gx.Model.Enums;
using Gx.Model.Other;

namespace Gx.Bll.Employee
{
    public class E_DepartmentInfoBll
    {  /// <summary>
       /// 得到树形部门  EmpBasicInfo/GetDepComBoTreeJson
       /// </summary>
       /// <param name="List"></param>
       /// <returns></returns>
        public List<TreeView> GetDepJson()
        {
            List<TreeView> tvList = new List<TreeView>();
            List<E_DepartmentInfo> depList = GetDepList();
            List<E_DepartmentInfo> oneList = depList.Where(x => x.ParentId == 0).ToList();
            TreeView tv = null;
            for (int i = 0; i < oneList.Count; i++)
            {
                tv = new TreeView()
                {
                    id = oneList[i].Id,
                    text = oneList[i].DepName,
                    children = new List<TreeView>(),
                };
                GetDepComBoTreeForeach(tv, depList, oneList[i].Id);
                tvList.Add(tv);
            }
            return tvList;
        }

        private void GetDepComBoTreeForeach(TreeView tv, List<E_DepartmentInfo> depList, int depId)
        {
            if (depList.Exists(x => x.ParentId == depId))
            {
                List<E_DepartmentInfo> _DepList = depList.Where(x => x.ParentId == depId).ToList();
                TreeView tvChild = null;
                foreach (var dep in _DepList)
                {
                    tvChild = new TreeView()
                    {
                        id = dep.Id,
                        text = dep.DepName,
                        children = new List<TreeView>(),
                    };
                    GetDepComBoTreeForeach(tvChild, depList, dep.Id);
                    tv.children.Add(tvChild);
                }
            }
        }

        private List<E_DepartmentInfo> GetDepList(string strWhere)
        {
            using (DepartmentInfoDal dal = new DepartmentInfoDal())
            {
                return dal.GetDepartmentInfoList(strWhere);
            }
        }

        /// <summary>
        /// 获取部门数据列表
        /// </summary>
        /// <returns></returns>
        public List<E_DepartmentInfo> GetDepList(int parentId = 0, int id = 0, string depName = "")
        {
            StringBuilder strWhere = new StringBuilder();
            if (id > 0)
            {
                strWhere.AppendFormat(" AND ID={0}", id);
            }
            if (!depName.IsNullOrEmpty())
            {
                strWhere.AppendFormat(" AND DepName='{0}'", depName);
            }
            if (parentId > 0)
            {
                strWhere.AppendFormat(" AND ParentId={0}", parentId);
            }
            return this.GetDepList(strWhere.ToString());
        }

        /// <summary>
        /// 获取部门数据列表
        /// </summary>
        /// <returns></returns>
        public List<E_DepartmentInfo> GetDepList()
        {
            return this.GetDepList("");
        }

        /// <summary>
        /// 添加部门信息
        /// </summary>
        /// <returns></returns>
        public ReturnModel<bool> AddDepartmentInfo(E_DepartmentInfo model)
        {
            using (DepartmentInfoDal dal = new DepartmentInfoDal())
            {
                ReturnModel<bool> r = new ReturnModel<bool>()
                {
                    Code = (int)Code.操作失败,
                    Msg = "操作失败",
                    Success = false,
                };
                if (model.DepName.IsNullOrEmpty())
                {
                    r.Msg = "部门名称不能为空";
                    return r;
                }
                bool res = dal.AddDepartmentInfo(model);
                if (res)
                {
                    new M_LogInfoBll().AddOptionsLog("AddDepartmentInfo", LoginHelper.GetUserId() + "添加一条班次信息", model);
                    r.Code = (int)Code.操作成功;
                    r.Msg = "操作成功";
                    r.Data = true;
                    r.Success = true;
                }
                return r;
            }
        }

        /// <summary>
        /// 编辑部门信息
        /// </summary>
        /// <returns></returns>
        public ReturnModel<bool> EditDepartmentInfo(E_DepartmentInfo model)
        {
            using (DepartmentInfoDal dal = new DepartmentInfoDal())
            {
                ReturnModel<bool> r = new ReturnModel<bool>()
                {
                    Code = (int)Code.操作失败,
                    Msg = "操作失败",
                    Success = false,
                };
                if (model.DepName.IsNullOrEmpty())
                {
                    r.Msg = "部门名称不能为空";
                    return r;
                }
                bool res = dal.EditDepartmentInfo(model);
                if (res)
                {
                    new M_LogInfoBll().AddOptionsLog("EditDepartmentInfo", LoginHelper.GetUserId() + "编辑一条班次信息", model);
                    r.Code = (int)Code.操作成功;
                    r.Msg = "操作成功";
                    r.Data = true;
                    r.Success = true;
                }
                return r;
            }
        }

        /// <summary>
        /// 删除部门信息
        /// </summary>
        /// <returns></returns>
        public ReturnModel<bool> DelDepartmentInfo(int id)
        {
            using (DepartmentInfoDal dal = new DepartmentInfoDal())
            {
                ReturnModel<bool> r = new ReturnModel<bool>()
                {
                    Code = (int)Code.操作失败,
                    Msg = "操作失败",
                    Success = false,
                };
                if (id == 1)
                {
                    r.Msg = "第一级部门不能删除!";
                    return r;
                }
                bool res = dal.DelDepartmentInfo(id);
                if (res)
                {
                    new M_LogInfoBll().AddOptionsLog("DelDepartmentInfo", LoginHelper.GetUserId() + "删除一条班次信息", id);
                    r.Code = (int)Code.操作成功;
                    r.Msg = "操作成功";
                    r.Data = true;
                    r.Success = true;
                }
                return r;
            }
        }

        public E_DepartmentInfo GetDepModel(int id)
        {
            using (DepartmentInfoDal dal = new DepartmentInfoDal())
            {
                return dal.GetDepartmentInfoModel(id);
            }
        }


        /// <summary>
        /// 根据id剔除我下面所有的子级
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public List<E_DepartmentInfo> GetDepList(int? id)
        {
            using (DepartmentInfoDal dal = new DepartmentInfoDal())
            {
                if (id == null) id = 0;
                return dal.GetDepartmentInfoList((int)id);
            }
        }
    }
}
